package com.preskool.serevice.impl;

import com.preskool.mapper.TransportMapper;
import com.preskool.po.Transport;
import com.preskool.po.TransportExample;
import com.preskool.serevice.TransportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Author:薛亮亮
 * @Date 2021/1/7 14:18
 * @Discription:校园大巴路线业务实现层
 */
@Service
@Slf4j
public class TransportServiceImpl implements TransportService {
    @Autowired
    private TransportMapper transportMapper;

    /**
     * @param transport 大巴的路线详细信息
     * @return 返回插入的数据的条数, 为1插入成功
     */
    @Override
    public int addTransport(Transport transport) {
//        Transport port =new Transport();
//        port.setRouteName(transport.getRouteName());
//        port.setVehicleNumber(transport.getVehicleNumber());
//        port.setDriverName(transport.getDriverName());
//        port.setContactNumber(transport.getContactNumber());
//        port.setDriverLicense(transport.getDriverLicense());
//        port.setDriverAddress(transport.getDriverAddress());

        int i = transportMapper.insertSelective(transport);
        return i;

    }

    /**
     * 删除Transport,按照路线删除
     * @param routeName 路线
     * @return
     */
    @Override
    public int deleteTransport(String routeName) {
        TransportExample example = new TransportExample();
        TransportExample.Criteria criteria = example.createCriteria();
        criteria.andRouteNameEqualTo(routeName);
        int i = transportMapper.deleteByExample(example);
        return i;
    }

    /**
     * @param offset   页码
     * @param limit    每页显示条数
     * @param sortName 按照是某个字段进行排序
     * @return
     */
    @Override
    public List<Transport> findTransports(Integer offset, Integer limit, String sortName) {
        TransportExample example = new TransportExample();
        //设置当前页码
        offset = (offset - 1) * limit;
        example.setOffset(offset);
        //设置每页显示条数
        example.setLimit(limit);
        //设置如何排序
        example.setOrderByClause(sortName);
        //查询
        List<Transport> transports = transportMapper.selectByExample(example);
        for (Transport transport : transports) {
            log.info("transport:"+transport);
        }
        return transports;
    }

    /**
     * 更新Transport数据
     * @param transport
     * @return
     */
    @Override
    public int updateTransport(Transport transport) {
        TransportExample example = new TransportExample();
        TransportExample.Criteria criteria = example.createCriteria();
        criteria.andRouteNameEqualTo(transport.getRouteName());
        int i = transportMapper.updateByExampleSelective(transport, example);
        return i;
    }
}
